package com.ruoyi.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: base-planthopper-detection-master_202130504106_李航
 * @description:
 * @author: LiHang
 * @create: 2021-09-24 09:08
 **/

public class ImageInfoTool {


    /**
     *
     * @param file
     * @return 返回的字符串格式举例：2020-08-19 09:09:39
     * @throws ImageProcessingException
     * @throws IOException
     */
    public static String ImageShootingTimeString(File file) throws ImageProcessingException, IOException {
        String imageName="";
        String[] array = new String[3];
        //如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(file)
                .getDirectories().forEach(v ->
                        v.getTags().forEach(t -> {
                            //System.out.println(t.getTagName() + " ： " + t.getDescription());
                            switch (t.getTagName()) {
                                //                    经度
                                case "GPS Longitude":
                                    array[0] = t.getDescription();
                                    break;
                                //                        纬度
                                case "GPS Latitude":
                                    array[1] = t.getDescription();
                                    break;
                                //                        拍摄时间
                                case "Date/Time Original":
                                    array[2] = t.getDescription();
                                default:
                                    break;
                            }
                        })
                );
        imageName=array[2];
        imageName=imageName.split(" ")[0].replace(":","-")+" "+imageName.split(" ")[1];
        return imageName;
    }

    /**
     *
     * @param file
     * @return 返回Date类型
     * @throws ImageProcessingException
     * @throws IOException
     */
    public static Date ImageShootingTimeDate(File file) throws ImageProcessingException, IOException, ParseException {
        Date date;
        String[] array = new String[3];
        //如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(file)
                .getDirectories().forEach(v ->
                        v.getTags().forEach(t -> {
                            //System.out.println(t.getTagName() + " ： " + t.getDescription());
                            switch (t.getTagName()) {
                                case "Date/Time Original":
                                    array[2] = t.getDescription();
                                default:
                                    break;
                            }
                        })
                );
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        if(array[2]!=null) {
            date = sdf.parse(array[2]);
        }else{
            date=new Date();
        }
        //System.out.println(date);
        return date;
    }
}
